﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ModifyPCenter : System.Web.UI.Page
{
    protected BLL.LostAndFound blllaf = new BLL.LostAndFound();
    protected void Page_Load(object sender, EventArgs e)
    {
            if (common.isLoginOrNot())
            {
                //显示相关要修改的信息
                if (!IsPostBack)
                {
                    if (Request.QueryString["id"] != null)
                    {
                        int lid = Convert.ToInt32(Request.QueryString["id"]);
                        List<MODEL.LostAndFound> models = new List<MODEL.LostAndFound>();
                        models = blllaf.getModelById(lid);
                        if (models[0].LostProperty == "寻物启事")
                        {
                            rdTypeF.Checked = true;
                        }
                        else
                        {
                            rdtype.Checked = true;
                        }
                        txtGsName.Value = models[0].GoodsName;
                        txtaGsAttr.Value = models[0].Feature;
                        txtAddress.Value = models[0].Address;
                        txtTime.Value = models[0].Time.ToString().Substring(0,10);

                    }
                }
                
            }
            else
            {
                Response.Redirect("index.aspx");
            }
       
    }

    #region 修改信息（启事内容）
    /// <summary>
    /// 修改信息（启事内容）
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnModify_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && checkVcode())
        {
            MODEL.LostAndFound model = new MODEL.LostAndFound();
            if (Request.QueryString["id"] != null)
            {
                model.LId = Convert.ToInt32(Request.QueryString["id"]); 
            }   
            model.UserId = ((MODEL.Users)Session["currentUser"]).UId;
            if (rdTypeF.Checked)
            {
                model.LostProperty = "寻物启事";
            }
            else
            {
                model.LostProperty = "招领启事";
            }
            model.GoodsName = txtGsName.Value;
            model.Feature = txtaGsAttr.Value;
            model.Address = txtAddress.Value;
            model.Time = Convert.ToDateTime(txtTime.Value);
            //图片
            #region  图片
            if (FUImg.HasFile)
            {
                //判断图片大小，不能超过500kb
                if (FUImg.PostedFile.ContentLength < 500 * 1024)
                {
                    //判断文件的后缀名，是否为图片
                    if (System.IO.Path.GetExtension(FUImg.FileName) == ".jpg" || System.IO.Path.GetExtension(FUImg.FileName) == ".gif" || System.IO.Path.GetExtension(FUImg.FileName) == ".png" || System.IO.Path.GetExtension(FUImg.FileName) == ".jpeg")
                    {
                        //model.Image = FUImg.FileName;
                        //string savePath = Server.MapPath("imgpublish/" + FUImg.FileName);//考虑到图片冲突的问题  所以不能用这方法
                        string s = Guid.NewGuid().ToString();
                        model.Image = s + System.IO.Path.GetExtension(FUImg.FileName);
                        string savePath = Server.MapPath("imgpublish/" + s + System.IO.Path.GetExtension(FUImg.FileName));
                        FUImg.SaveAs(savePath);
                    }
                    else
                    {
                        model.Image = "";
                    }
                }
                else
                {
                    model.Image = "";
                }
            }
            //如果没有重新上传图片，则图片为原来那张
            else
            {
                if (Request.QueryString["id"] != null)
                {
                    int lid2 = Convert.ToInt32(Request.QueryString["id"]);
                    List<MODEL.LostAndFound> models = blllaf.getModelById(lid2);
                    model.Image = models[0].Image;
                }
            }
            #endregion

            if (blllaf.modifyInfo(model))
            {
                MessageBox.Show(this, "修改启事信息成功");
                //发布信息成功后，跳转到具体信息展示页面
                int lid3 = Convert.ToInt32(Request.QueryString["id"]);
                MessageBox.script(this, "window.location='detail.aspx?id=" + lid3.ToString() + "';");
            }
            else
            {
                MessageBox.Show(this, "修改启事信息失败，请重试");
            }
        }
    }
    #endregion

    #region 检查验证码是否正确
    /// <summary>
    /// 检查验证码是否正确
    /// </summary>
    /// <returns></returns>
    private bool checkVcode()
    {
        if (Session["vCode"] != null)
        {
            if (txtVCode.Value.Trim() == Session["vCode"].ToString())
            {
                return true;
            }
            else
            {
                MessageBox.Show(this, "验证码错误");
                return false;
            }
        }
        else
        {
            MessageBox.Show(this, "验证码过期了");
            return false;
        }
    }
    #endregion

    
}